package Screens;

import Utils.SoundManager;

import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.acumenvn.pent.MyPent;

public class StoryScreen implements Screen {

	Sound mSoundClick;
	float WIDTH = 400;
	float HEIGHT = 300;
	MyPent mGame;
	Texture mTexture;
	Sprite mSprite;
	SpriteBatch mBatch;
	float timeToLoad = 1;
	private OrthographicCamera mCamera;
	private Rectangle glViewport;
	float rotationSpeed;
	InputProcessor mInputP = new InputProcessor() {
		
		@Override
		public boolean touchUp(int screenX, int screenY, int pointer, int button) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean touchDragged(int screenX, int screenY, int pointer) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean touchDown(int screenX, int screenY, int pointer, int button) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean scrolled(int amount) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean mouseMoved(int screenX, int screenY) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean keyUp(int keycode) {
			// TODO Auto-generated method stub
			
			return false;
		}
		
		@Override
		public boolean keyTyped(char character) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean keyDown(int keycode) {
			// TODO Auto-generated method stub
			if(keycode == Keys.ENTER) {
				SoundManager.getInstance().playSound(mSoundClick);
				mGame.setScreen(new GameScreen(mGame));
				return true;
			}
			return false;
		}
	};
	
	public StoryScreen(MyPent game) {
		// TODO Auto-generated constructor stub
		this.mGame = game;
		mCamera = new OrthographicCamera(WIDTH, HEIGHT);
		mCamera.position.set(0, 0, 0);
		glViewport = new Rectangle(0, 0, WIDTH, HEIGHT);
	}
	
	@Override
	public void render(float delta) {
		// TODO Auto-generated method stub
		handleInput();
		Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
		Gdx.gl20.glViewport((int)glViewport.x, (int)glViewport.y, (int)glViewport.width, (int)glViewport.height);
		
		mBatch.begin();
		mSprite.draw(mBatch);
		mBatch.end();
		mCamera.update();
		mCamera.apply(Gdx.gl10);
		
		mCamera.zoom = 2;
//		if(timeToLoad > 0) {
//			timeToLoad -= delta;
//			return;
//		}
//		mGame.setScreen(new GameScreen(mGame));
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		mTexture = new Texture("Images/Store.png");
		mTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		mSprite = new Sprite(mTexture);
		mSprite.setBounds(0, 0, 800, 600);
		
		mBatch = new SpriteBatch();
		Gdx.graphics.setContinuousRendering(false);
		Gdx.graphics.requestRendering();
		Gdx.input.setInputProcessor(mInputP);
		
		mSoundClick = SoundManager.getInstance().getSoundSelectMenu();
		timeToLoad = 1;
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		mBatch.dispose();
		mTexture.dispose();
	}
	
	private void handleInput() {
		if(Gdx.input.isKeyPressed(Keys.A)) {
            mCamera.zoom += 0.02;
		}
		if(Gdx.input.isKeyPressed(Keys.Q)) {
			mCamera.zoom -= 0.02;
		}
		if(Gdx.input.isKeyPressed(Keys.LEFT)) {
            if (mCamera.position.x > 0)
            	mCamera.translate(-3, 0, 0);
		}
		if(Gdx.input.isKeyPressed(Keys.RIGHT)) {
            if (mCamera.position.x < 1024)
            	mCamera.translate(3, 0, 0);
		}
		if(Gdx.input.isKeyPressed(Keys.DOWN)) {
            if (mCamera.position.y > 0)
            	mCamera.translate(0, -3, 0);
		}
		if(Gdx.input.isKeyPressed(Keys.UP)) {
            if (mCamera.position.y < 1024)
            	mCamera.translate(0, 3, 0);
		}
		if(Gdx.input.isKeyPressed(Keys.W)) {
			mCamera.rotate(-rotationSpeed, 0, 0, 1);
		}
		if(Gdx.input.isKeyPressed(Keys.E)) {
			mCamera.rotate(rotationSpeed, 0, 0, 1);
		}
	}

}
